//
//  UIContactsInfoViewController.m
//  TTAddressBook
//
//  Created by iphonekf2 on 12-1-30.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "UITimeObjViewController.h"
#import "imagePath.h"


@implementation UITimeObjViewController

@synthesize TimeObjInfo;
@synthesize tv_name;
@synthesize tv_location;
@synthesize tv_shareMemo;
@synthesize tv_memo;
@synthesize dp_beginDate,dp_beginTime;
@synthesize dp_endDate,dp_endTime;
@synthesize shareToBoss;
@synthesize shareToCustomer;
@synthesize iType;
@synthesize obj_Type;
@synthesize obj_Kind;
@synthesize retArr;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil TimeObj:(NSString*) TimeObjID objType:(NSString*) objType PersonName:(NSString*) personName given:(NSString*) given;
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {   
        isGiven = [given isEqualToString:@"true"];
        //6、通过ID获取计划 /getPlanById
        if ([objType isEqualToString:@"plan"]) {
            self.TimeObjInfo = [[MyClass getInstance] getPlan:TimeObjID];
            self.iType=1;
            self.title=personName;
        }
        else
        {
            self.TimeObjInfo = [[MyClass getInstance] getDeed:TimeObjID];
            self.iType=2;
            self.title=personName;
        }
        if ([[TimeObjInfo objectForKey:@"code"] intValue]==0) {
            self.obj_Type = [TimeObjInfo objectForKey:@"type"];
            self.obj_Kind = [TimeObjInfo objectForKey:@"kind"];
            MyClass *gs = [MyClass getInstance];
            self.retArr = [gs getResultMeta:obj_Type ofKind:obj_Kind];
            showMemo = [[TimeObjInfo objectForKey:@"editable"] isEqualToString:@"true"];
            if ([personName isEqualToString:gs.userName]) 
            {
                showShareMemo = NO;
            }
            else 
            {
                showShareMemo = YES;
            }
        }
        else {
            self.TimeObjInfo=nil;
            NSLog(@"%@",[TimeObjInfo objectForKey:@"error"]);
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"温馨提示" message: [NSString stringWithFormat:@"%@",[TimeObjInfo objectForKey:@"error"]] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil ];
            [alert show];
        }
    }
    [self setHidesBottomBarWhenPushed:YES];
    return self;
}
- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
-(void)btnSave{
    [TimeObjInfo setValue:tv_name.text forKey:@"name"];
    [TimeObjInfo setValue:tv_location.text forKey:@"location"];
    if (showMemo) 
        [TimeObjInfo setValue:tv_memo.text forKey:@"memo"];
    [TimeObjInfo setValue:tv_shareMemo.text forKey:@"shareMemo"];
    
    //实例化一个NSDateFormatter对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设定时间格式,这里可以设置成自己需要的格式
    
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    //用[NSDate date]可以获取系统当前时间
    NSString* str1 = [dateFormatter stringFromDate:dp_beginDate.date];
    [dateFormatter setDateFormat:@"HH:mm"];
    NSString* str2 = [dateFormatter stringFromDate:dp_beginTime.date];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    //用[NSDate date]可以获取系统当前时间
    NSString* str3 = [dateFormatter stringFromDate:dp_endDate.date];
    [dateFormatter setDateFormat:@"HH:mm"];
    NSString* str4 = [dateFormatter stringFromDate:dp_endTime.date];
    
    NSString* strBegin  = [NSString stringWithFormat:@"%@ %@",str1,str2];
    NSString* strEnd  = [NSString stringWithFormat:@"%@ %@",str3,str4];
    
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    NSLocale *enLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
    [format setLocale:enLocale];
    [enLocale release];
    [format setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSDate *begin = [format dateFromString:strBegin];
    NSDate *end = [format dateFromString:strEnd]; 
    
    //  NSLog(@"开始时间%@ 结束时间%@",strBegin,strEnd);
    
    [dateFormatter release];
    [TimeObjInfo setValue:begin  forKey:@"begin"];
    
    [TimeObjInfo setValue:end forKey:@"end"];  
    if (shareToCustomer)
        [TimeObjInfo setValue:@"true" forKey:@"shareToCustomer"];
    else
        [TimeObjInfo setValue:@"false" forKey:@"shareToCustomer"];
    
    if (shareToBoss)
        [TimeObjInfo setValue:@"true" forKey:@"shareToBoss"];
    else
        [TimeObjInfo setValue:@"false" forKey:@"shareToBoss"];
    NSDictionary* ObjInfo;
    if (iType==1) {
        ObjInfo = [[MyClass getInstance] editPlan:TimeObjInfo];
    }
    else
        ObjInfo = [[MyClass getInstance] editDeed:TimeObjInfo];
    if ([[ObjInfo objectForKey:@"code"] intValue]==0) {
        [self popSendMsgRetView:0];   
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message: [NSString stringWithFormat:@"%@",[ObjInfo objectForKey:@"error"]] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil ];
        [alert show];
        [alert release];
    }
    
}
#pragma mark - View lifecycle
-(void)btnSaveNoPop{
    [TimeObjInfo setValue:tv_name.text forKey:@"name"];
    [TimeObjInfo setValue:tv_location.text forKey:@"location"];
    if (showMemo) 
        [TimeObjInfo setValue:tv_memo.text forKey:@"memo"];
    [TimeObjInfo setValue:tv_shareMemo.text forKey:@"shareMemo"];
    
    //实例化一个NSDateFormatter对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设定时间格式,这里可以设置成自己需要的格式
    
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    //用[NSDate date]可以获取系统当前时间
    NSString* str1 = [dateFormatter stringFromDate:dp_beginDate.date];
    [dateFormatter setDateFormat:@"HH:mm"];
    NSString* str2 = [dateFormatter stringFromDate:dp_beginTime.date];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    //用[NSDate date]可以获取系统当前时间
    NSString* str3 = [dateFormatter stringFromDate:dp_endDate.date];
    [dateFormatter setDateFormat:@"HH:mm"];
    NSString* str4 = [dateFormatter stringFromDate:dp_endTime.date];
        
    NSString* strBegin  = [NSString stringWithFormat:@"%@ %@",str1,str2];
    NSString* strEnd  = [NSString stringWithFormat:@"%@ %@",str3,str4];
    
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    NSLocale *enLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
    [format setLocale:enLocale];
    [enLocale release];
    [format setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSDate *begin = [format dateFromString:strBegin];
    NSDate *end = [format dateFromString:strEnd]; 

  //  NSLog(@"开始时间%@ 结束时间%@",strBegin,strEnd);
    
    [dateFormatter release];
    [TimeObjInfo setValue:begin  forKey:@"begin"];

    [TimeObjInfo setValue:end forKey:@"end"];  
    if (shareToCustomer)
        [TimeObjInfo setValue:@"true" forKey:@"shareToCustomer"];
    else
        [TimeObjInfo setValue:@"false" forKey:@"shareToCustomer"];
    
    if (shareToBoss)
        [TimeObjInfo setValue:@"true" forKey:@"shareToBoss"];
    else
        [TimeObjInfo setValue:@"false" forKey:@"shareToBoss"];
    NSDictionary* ObjInfo;
    if (iType==1) {
        ObjInfo = [[MyClass getInstance] editPlan:TimeObjInfo];
    }
    else
        ObjInfo = [[MyClass getInstance] editDeed:TimeObjInfo];
    if ([[ObjInfo objectForKey:@"code"] intValue]==0) {
        [self.navigationController popViewControllerAnimated:YES];
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message: [NSString stringWithFormat:@"%@",[ObjInfo objectForKey:@"error"]] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil ];
        [alert show];
        [alert release];
    }

}
- (void)viewDidLoad
{
    [super viewDidLoad];
    tableview.delegate=self;
	tableview.dataSource=self;
    for(UIView *subview in [self.navigationController.view  subviews]){
        if (subview.tag==9999)
            [subview removeFromSuperview];
	}
    if ([[TimeObjInfo objectForKey:@"editable"] isEqualToString:@"true"]){
//        if (iType==1) {
//            self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"navigation_bar_icon_00.png"]
//                                                                                       style:UIBarButtonItemStyleBordered target:self action:@selector(showMenu)] autorelease];
//        }
//        else 
            self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStyleBordered target:self action:@selector(btnSave)] autorelease];
    }
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"返回"
                                                                              style:UIBarButtonItemStyleBordered target:self action:@selector(showMenu)] autorelease];
    
    popTipView = [[UIView alloc] initWithFrame:CGRectMake(90, 50, 140, 40)];
    popTipView.layer.cornerRadius = 6;
    popTipView.layer.masksToBounds = YES;
    popTipView.backgroundColor = [UIColor blackColor];
    popTipView .alpha = 0.0f;
    
    UILabel* popLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 140, 40)];
    popLabel.tag = 20;
    popLabel.textAlignment = UITextAlignmentCenter;
    popLabel.backgroundColor = [UIColor blackColor];
    popLabel.font = [UIFont systemFontOfSize:14.0];
    popLabel.textColor = [UIColor whiteColor];
    popLabel.text = @"该日程不可编辑！";
    [popTipView addSubview:popLabel];
    [popLabel release];
    
    [self.view addSubview:popTipView];
    
    [self setHidesBottomBarWhenPushed:YES];
}
-(void)dealloc{
    [popTipView release];
    self.obj_Type =nil;
    self.obj_Kind=nil;
    self.retArr=nil;
    self.TimeObjInfo=nil;
    self.dp_beginDate=nil;
    self.dp_beginTime=nil;
    self.dp_endDate=nil;
    self.dp_endTime=nil;
}
- (void)viewDidUnload
{
    [super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 24;
}
-(void) HideKey{
    [tv_name resignFirstResponder];
    if (tv_location) {
        [tv_location resignFirstResponder];
    }
    if (tv_shareMemo) 
    {
        [tv_shareMemo resignFirstResponder];
    }
    if (tv_memo){
        [tv_memo resignFirstResponder];
    }
}
-(void)scrollViewwill:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{

}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == sectionNum-1) {
        return 255;
    }
    return 10;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    NSInteger num=0;
    if (section>0) {
        myHeadView*  headView = [[[myHeadView alloc] initWithFrame:CGRectMake(0, 0, 320, 24)] autorelease];
        headView.backgroundColor = [UIColor clearColor];
        [headView set_Delegate:self];
        if (section>=2 && section<=5)
        {
            UILabel* lab1 = [[UILabel alloc] init];
            lab1.frame = CGRectMake(20, 0, 80, 20);
            if (!showShareMemo) {
                num++;
            }
            if (!showMemo) {
                num++;
            }
            if (section==2) 
            {
                lab1.text = @"位置";
            }
            if (section==3 && showShareMemo) 
            {
                lab1.text = @"共享备注";
            }
            else if(section==(4-num) && showMemo)
            {
                lab1.text = @"私密备注";
            }
            else if(section== (5-num) && isGiven)
            {
                lab1.text = @"开放属性";
            }
            lab1.font = [UIFont boldSystemFontOfSize:18];
            lab1.backgroundColor = [UIColor clearColor];           
            lab1.tag = 1001;
            
            [headView addSubview:lab1];
            [lab1 release]; 
        }
        
        //UIButton * btnHide;
//        btnHide=[UIButton buttonWithType:UIButtonTypeCustom];
//        btnHide.tag=1001;
//        [btnHide addTarget:self action:@selector(HideKey) forControlEvents:UIControlEventTouchUpInside];
//        [headView addSubview:btnHide];
//        btnHide.backgroundColor = [UIColor clearColor];
//        btnHide.frame=CGRectMake(0, 0, 320, 24);    
        return headView;
    }
    return nil;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (section>0) {
        myHeadView*  headView = [[[myHeadView alloc] initWithFrame:CGRectMake(0, 0, 320, 24)] autorelease];
        headView.backgroundColor = [UIColor clearColor];
        [headView set_Delegate:self];
//        UIButton * btnHide;
//        btnHide=[UIButton buttonWithType:UIButtonTypeCustom];
//        btnHide.tag=1001;
//        [btnHide addTarget:self action:@selector(HideKey) forControlEvents:UIControlEventTouchUpInside];
//        [headView addSubview:btnHide];
//        btnHide.backgroundColor = [UIColor clearColor];
//        btnHide.frame=CGRectMake(0, 0, 320, 10);    
        return headView;
    }
    return nil;
}
-(void)myTouchEnd{
    [self HideKey];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSInteger num=0;
    if (!showShareMemo) {
        num++;
    }
    if (!showMemo) {
        num++;
    }
    if (isGiven) {
        num++;
    }
    if (TimeObjInfo) {
        sectionNum = 6-num;
        return sectionNum;
    }
    else
        return 0;
}
-(void) showMenu{
    if (iType!=1) 
    {
        if ([[TimeObjInfo objectForKey:@"editable"] isEqualToString:@"true"])
        {
            UIButton* backgroundBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
            [backgroundBtn addTarget:self action:@selector(hideMenu) forControlEvents:UIControlEventTouchUpInside]; 
            backgroundBtn.backgroundColor = [UIColor clearColor];
            backgroundBtn.tag = MENUVIEW_TAG;
            [self.navigationController.view addSubview:backgroundBtn];
            [backgroundBtn release];
            UICustomView* MenuView = [[UICustomView alloc] initWithFrame:CGRectMake(10, 55, 147, 87) :nil :nil :@"不保存修改":@"确认修改并返回" :nil];
            [self.navigationController.view addSubview:MenuView];
            MenuView.tag = MENUVIEW_TAG;
            [MenuView set_Delegate:self];
            [MenuView release];
            isConfirm = NO;
        }
        else
            [self.navigationController popViewControllerAnimated:YES];
    }
    else
    {
        if ([[TimeObjInfo objectForKey:@"editable"] isEqualToString:@"true"]){ 
            UIButton* backgroundBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
            [backgroundBtn addTarget:self action:@selector(hideMenu) forControlEvents:UIControlEventTouchUpInside]; 
            backgroundBtn.backgroundColor = [UIColor clearColor];
            backgroundBtn.tag = MENUVIEW_TAG;
            [self.navigationController.view addSubview:backgroundBtn];
            [backgroundBtn release];
            UICustomView* MenuView = [[UICustomView alloc] initWithFrame:CGRectMake(10, 55, 147, 127) :nil :nil :@"不保存修改":@"确认修改并返回" :@"登记完成并返回"];
            [self.navigationController.view addSubview:MenuView];
            MenuView.tag = MENUVIEW_TAG;
            [MenuView set_Delegate:self];
            [MenuView release];
            isConfirm = NO;
        }
        else
            [self.navigationController popViewControllerAnimated:YES];
    }
}
-(void) btnFinishPlan{
    [TimeObjInfo setValue:tv_name.text forKey:@"name"];
    [TimeObjInfo setValue:tv_location.text forKey:@"location"];
    if (showMemo)
        [TimeObjInfo setValue:tv_memo.text forKey:@"memo"];
    [TimeObjInfo setValue:tv_shareMemo.text forKey:@"shareMemo"];
    
    //实例化一个NSDateFormatter对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设定时间格式,这里可以设置成自己需要的格式
    
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    //用[NSDate date]可以获取系统当前时间
    NSString* str1 = [dateFormatter stringFromDate:dp_beginDate.date];
    [dateFormatter setDateFormat:@"HH:mm"];
    NSString* str2 = [dateFormatter stringFromDate:dp_beginTime.date];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    //用[NSDate date]可以获取系统当前时间
    NSString* str3 = [dateFormatter stringFromDate:dp_endDate.date];
    [dateFormatter setDateFormat:@"HH:mm"];
    NSString* str4 = [dateFormatter stringFromDate:dp_endTime.date];
    
    NSString* strBegin  = [NSString stringWithFormat:@"%@ %@",str1,str2];
    NSString* strEnd  = [NSString stringWithFormat:@"%@ %@",str3,str4];
    
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    NSLocale *enLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
    [format setLocale:enLocale];
    [enLocale release];
    [format setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSDate *begin = [format dateFromString:strBegin];
    NSDate *end = [format dateFromString:strEnd]; 
    
    //  NSLog(@"开始时间%@ 结束时间%@",strBegin,strEnd);
    
    [dateFormatter release];
    [TimeObjInfo setValue:begin  forKey:@"begin"];
    
    [TimeObjInfo setValue:end forKey:@"end"];  
    if (shareToCustomer)
        [TimeObjInfo setValue:@"true" forKey:@"shareToCustomer"];
    else
        [TimeObjInfo setValue:@"false" forKey:@"shareToCustomer"];
    
    if (shareToBoss)
        [TimeObjInfo setValue:@"true" forKey:@"shareToBoss"];
    else
        [TimeObjInfo setValue:@"false" forKey:@"shareToBoss"];
    BOOL haveRet = NO;
    if (self.retArr)
    {
        if ([self.retArr allValues]>0) {
            haveRet= YES;
        }
    }
    if (!haveRet) {
        if (!isConfirm) 
        {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"温馨提示" 
                                                           message: @"是否需要登记完成？" 
                                                          delegate:self 
                                                 cancelButtonTitle:@"取消" 
                                                 otherButtonTitles:@"确定", nil ];
            [alert show];
            [alert release];
        }
        if (isConfirm) 
        {
            [TimeObjInfo setValue:@"0" forKey:@"result"];
            NSDictionary*  ObjFinish = [[MyClass getInstance] finishPlan:TimeObjInfo];
            if ([[ObjFinish objectForKey:@"code"] intValue]==0) {
                //NSLog(@"%@",ObjFinish);
                [self.navigationController popViewControllerAnimated:YES];
            }
            else
            {
                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"温馨提示" message: [NSString stringWithFormat:@"%@",[ObjFinish objectForKey:@"error"]] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil ];
                [alert show];
                [alert release];
            }
        }
    }
    else 
    {
        UIGroupViewController * view = [[UIGroupViewController alloc] initWithResultList:retArr :TimeObjInfo];        
        [self.navigationController pushViewController:view animated:YES];
        [view release];
    }
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if ( buttonIndex == 1 ) 
    {
        isConfirm = YES;
        [self btnFinishPlan];
    }
    else
    {
        isConfirm = NO; 
    }
}
-(void) ChooseWithFlag:(NSInteger)Flag{
    [self hideMenu];
    switch (Flag) {
        case 1:
            [self.navigationController popViewControllerAnimated:YES];
            break;
            
        case 2:
            [self btnSaveNoPop];
            break;
        case 3:
            [self btnFinishPlan];
            break;
        default:
            break;
    }
}
-(void)popSendMsgRetView:(int)code {
    UILabel* contentLabel = (UILabel*)[popTipView viewWithTag:20];
    if(code==0){
        contentLabel.text = @"保存成功！";
    }
    else if (code == -1) {
        contentLabel.text = @"该日程不可编辑！";
    }
    else if(code==-2){
        contentLabel.text = @"保存失败！";
    }
    else
    {
        contentLabel.text = @"未知错误！";
    }
    popTipView.frame = CGRectMake(90,30,140,40);
    popTipView.alpha = 0.8f;    
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration: 2.0];
    
    popTipView.alpha = 0.0f;
    
    [UIView commitAnimations];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger iHeight;
    iHeight = 44;	
    if (showShareMemo) {
        if (indexPath.section==3)
            iHeight = 88;
        if (showMemo) {
            if (indexPath.section==4) {
                iHeight = 88;
            }
        }
    }
    else 
    {
        if (showMemo) {
            if (indexPath.section==3) {
                iHeight = 88;
            }
        }
    }
    return iHeight;
}

- (NSInteger)tableView:(UITableView *)tableView  numberOfRowsInSection:(NSInteger)section
{
    if (!TimeObjInfo) {
        return 0;
    }
    NSInteger num=0;
    if (!showShareMemo) {
        num++;
    }
    if (!showMemo) {
        num++;
    }
    NSInteger count=1;
    switch (section) {
        case 0:
            if (iType==2)
            count=2;
            else {
                count=1;
            }
            break;
        case 1:
            count=2;
            break;
        case 2:
            count=1;
            break;
        case 3:
            count=(num==2 ? 2:1);
            break;
        case 4:
            count= (num==1 ? 2:1);
            break;
        case 5:
            count=2;
            break;
        case 6:
            count=1;
            break;
        default:
            break;
    }
    return count;
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    UITableViewCell * cell= ((UITableViewCell*)[textView superview]); 
    CGPoint point = tableview.contentOffset;
    curPoint = point;
    point.y = cell.frame.origin.y-30;
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    [UIView setAnimationDelegate:self];
    tableview.contentOffset = point;
    //NSLog(@"%f %f",self.view.center.x,self.view.center.y);
    //self.view.center = CGPointMake(160, 20);
    [UIView commitAnimations];
    return YES;
}

-(void)showDate:(id)sender{
    [tv_name resignFirstResponder];
    [tv_location resignFirstResponder];
    [tv_memo resignFirstResponder];
    [tv_shareMemo resignFirstResponder];
    DatePicker* dp =(DatePicker*)[sender superview];
    if ([dp getSubViewTag]==0) {//若tag标志等于0，说明datepicker未显示
        [dp showSubView];       
    }

}
- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger section = [indexPath section];
    NSUInteger row = [indexPath row];
    UITableViewCell *cell;
    NSInteger num=0;
    if (!showMemo) {
        num++;
    }
    if (!showShareMemo) {
        num++;
    }
    if (section==0 && row==1) 
    {
        static NSString *subViewCellIdentifier = @"subViewCell";
        cell = [tableView dequeueReusableCellWithIdentifier:
                subViewCellIdentifier ];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
                                           reuseIdentifier: subViewCellIdentifier ] autorelease];
            cell.accessoryType = UITableViewCellAccessoryNone;
        } 
    }
    else
    {
        static NSString *SectionsOneIdentifier = @"SectionsOneIdentifier";
        cell = [tableView dequeueReusableCellWithIdentifier:
                SectionsOneIdentifier ];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                           reuseIdentifier: SectionsOneIdentifier ] autorelease];
            cell.accessoryType = UITableViewCellAccessoryNone;
        }    
    }
    for (UIView *subview in cell.subviews) {
        if (subview.tag >= 1001)              
            [subview removeFromSuperview];
    }
     cell.selectionStyle = UITableViewCellSelectionStyleNone;
     cell.textLabel.text=nil;
     cell.accessoryType = UITableViewCellAccessoryNone;
    if (section==0) {
        if (row==0) {
            cell.textLabel.text=@"标题";
            self.tv_name=[[UITextView alloc] initWithFrame:CGRectMake(60, 3, 245, 27)];
            tv_name.font=[UIFont systemFontOfSize:14];
            tv_name.backgroundColor=[UIColor clearColor];
            tv_name.tag=1001;
            [cell addSubview:tv_name];
            [tv_name release];
            tv_name.text =  [TimeObjInfo objectForKey:@"name"];
        }
        else{
            cell.textLabel.text=@"完成情况";
            cell.detailTextLabel.font = [UIFont systemFontOfSize:14];
            //cell.accessoryType =  UITableViewCellAccessoryDisclosureIndicator;
            NSString* strCodeResult = [TimeObjInfo objectForKey:@"result"];
            NSString* strNameResult = [[MyClass getInstance] getResultText:obj_Type ofKind:obj_Kind ofResult:strCodeResult];           
            UILabel* lab1; 
            lab1 = [[UILabel alloc] init];
            lab1.frame = CGRectMake(240, 10, 60, 22);
            if ([strNameResult length]<=0 && [[TimeObjInfo objectForKey:@"result"] intValue]==0 && iType==2) {
              lab1.text=@"已经完成";    
            }
            else
                lab1.text = strNameResult;
            lab1.font = [UIFont boldSystemFontOfSize:14];
            lab1.backgroundColor = [UIColor clearColor];           
            lab1.tag = 1001;
            [cell addSubview:lab1];
            lab1.textColor = [UIColor grayColor];
            [lab1 release]; 
            //cell.detailTextLabel.text = strNameResult;
        }
        
    }
    else if(section==1)
    {
        NSDate * date1 = [TimeObjInfo objectForKey:@"begin"];
        NSDate * date2 = [TimeObjInfo objectForKey:@"end"];
        if (row==0) {
            cell.textLabel.text =  @"开始时间";
            if (!self.dp_beginDate)
            self.dp_beginDate=[[DatePicker alloc]initWithFrame:CGRectMake (105,10,60,35)];
            [dp_beginDate setDate:date1];
            [cell addSubview:dp_beginDate];
            dp_beginDate.datePickerMode=UIDatePickerModeDate;
            NSDateFormatter* df=[[NSDateFormatter alloc]init];
            [df setDateFormat:@"yyyy-MM-dd"];
            dp_beginDate.dateFormatter=df;
            dp_beginDate.tag=1001;
            [dp_beginDate.btnDate addTarget:self action:@selector(showDate:) forControlEvents:(UIControlEventTouchUpInside)];
            if (!self.dp_beginTime)
            self.dp_beginTime=[[DatePicker alloc]initWithFrame:CGRectMake (195,10,60,35)];
            [dp_beginTime setDate:date1];
            [cell addSubview:dp_beginTime];
            dp_beginTime.datePickerMode=UIDatePickerModeTime;
            NSDateFormatter* df1=[[NSDateFormatter alloc]init];
            [df1 setDateFormat:@"HH:mm:ss"];
            dp_beginTime.dateFormatter=df1;
            dp_beginTime.tag=1001;
            [dp_beginTime.btnDate addTarget:self action:@selector(showDate:) forControlEvents:(UIControlEventTouchUpInside)];
            //[dp_begin release];   
        }
        else if (row == 1)
        {
            cell.textLabel.text =  @"结束时间";
            if (!self.dp_endDate)
            self.dp_endDate=[[DatePicker alloc]initWithFrame:CGRectMake (105,8,80,35)];
            [dp_endDate setDate:date2];
            [cell addSubview:dp_endDate];
            dp_endDate.datePickerMode=UIDatePickerModeDate;
            NSDateFormatter* df=[[NSDateFormatter alloc]init];
            [df setDateFormat:@"yyyy-MM-dd"];
            dp_endDate.dateFormatter=df;
            dp_endDate.tag=1002;
            [dp_endDate.btnDate addTarget:self action:@selector(showDate:) forControlEvents:(UIControlEventTouchUpInside)];
            if (!self.dp_endTime)
            self.dp_endTime=[[DatePicker alloc]initWithFrame:CGRectMake (195,8,80,35)];
            [dp_endTime setDate:date2];
            [cell addSubview:dp_endTime];
            dp_endTime.datePickerMode=UIDatePickerModeTime;
            NSDateFormatter* df1=[[NSDateFormatter alloc]init];
            [df1 setDateFormat:@"HH:mm:ss"];
            dp_endTime.dateFormatter=df1;
            dp_endTime.tag=1002;
            [dp_endTime.btnDate addTarget:self action:@selector(showDate:) forControlEvents:(UIControlEventTouchUpInside)];
            //[dp_end release];
        }
    }
    else if(section==2) {
//        cell.textLabel.text=@"位置";
        self.tv_location=[[UITextView alloc] initWithFrame:CGRectMake(10, 3, 300, 27)];
        tv_location.font=[UIFont systemFontOfSize:14];
        tv_location.backgroundColor=[UIColor clearColor];
        tv_location.tag=1001;
        [cell addSubview:tv_location];
        [tv_location release];
        tv_location.delegate = self;
        tv_location.text= [TimeObjInfo objectForKey:@"location"];
        if ([tv_location.text length]<=0) {
            tv_location.text=@" ";
        }
        
    }
    else if(section==3 && showShareMemo)
    {
        self.tv_shareMemo=[[UITextView alloc] initWithFrame:CGRectMake(10, 0, 300, 80)];
        tv_shareMemo.font=[UIFont systemFontOfSize:14];
        tv_shareMemo.backgroundColor=[UIColor clearColor];
        tv_shareMemo.tag=1001;
        [cell addSubview:tv_shareMemo];
        tv_shareMemo.delegate=self;
        [tv_shareMemo release];
        tv_shareMemo.text= [TimeObjInfo objectForKey:@"shareMemo"];
    }
    else if(section== (4-num) && showMemo)
    {
        //if (iType==1) 
        {
            self.tv_memo=[[UITextView alloc] initWithFrame:CGRectMake(10, 2, 300, 78)];
            tv_memo.font=[UIFont systemFontOfSize:14];
            tv_memo.delegate=self;
            tv_memo.backgroundColor = [UIColor clearColor];
            tv_memo.tag=1001;
            [cell addSubview:tv_memo];
            [tv_memo release];
            tv_memo.text= [TimeObjInfo objectForKey:@"memo"];
        }
    }
    else if(section==(5-num))
    {
        if(row==0){
            cell.textLabel.text=@"开放给主管";
            NSString* str = [TimeObjInfo objectForKey:@"shareToBoss"];
            if ([str isEqualToString:@"true"])
                cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
        else if(row==1){
            cell.textLabel.text=@"开放给客户";
            NSString* str= [TimeObjInfo objectForKey:@"shareToCustomer"];
            if ([str isEqualToString:@"true"])
                cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
    }
    return cell;
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    [self HideKey];
}
-(void)viewWillAppear:(BOOL)animated{
    [tableview reloadData];
}
-(void)viewDidAppear:(BOOL)animated{
    if ([[TimeObjInfo objectForKey:@"editable"] isEqualToString:@"true"]) {
        //
    }
    else
        [self popSendMsgRetView:-1];
}
-(void)composFreeMessage{
    composeMessageViewController* freeMessageController = [[composeMessageViewController alloc] init];
    [self.navigationController pushViewController:freeMessageController animated:YES];
    [freeMessageController release];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tv_name resignFirstResponder];
    
    if (tv_location) {
        [tv_location resignFirstResponder];
    }
    if (tv_shareMemo) 
    {
        [tv_shareMemo resignFirstResponder];
    }
    if (tv_memo){
        [tv_memo resignFirstResponder];
    }
    NSInteger num=0;
    if (!showShareMemo) {
        num++;
    }
    if (!showMemo) {
        num++;
    }
    if (indexPath.section== (5-num)) {
        if (indexPath.row>=0)
        {
            UITableViewCell *cell;
            cell = [tableView cellForRowAtIndexPath:indexPath];
            if (cell.accessoryType ==UITableViewCellAccessoryCheckmark) {
                cell.accessoryType =UITableViewCellAccessoryNone;
                if (indexPath.row==0)
                    self.shareToBoss= NO;
                else 
                {
                    self.shareToCustomer = NO;
                }
            }
            else{
                cell.accessoryType =UITableViewCellAccessoryCheckmark;
                if (indexPath.row==0)
                    self.shareToBoss= YES;
                else 
                {
                    self.shareToCustomer = YES;
                }
            }
        }
    }
//    if (indexPath.section==0 && indexPath.row==10000) {
//        
//        MyClass *gs = [MyClass getInstance];
//        NSDictionary * ret = [gs getResultMeta:obj_Type ofKind:obj_Kind];
//        if ([[ret objectForKey:@"code"] intValue] == 0) {
//            retArr = [ret objectForKey:@"list"];
//        }
//        UIGroupViewController * view = [[UIGroupViewController alloc] initWithResultList:retArr];        
//        [self.navigationController pushViewController:view animated:YES];
//        [view release];
//    }
}



@end
